#include "Application.h"

int Application::InitializeTextures()
{
	int i,j,cnt,pcnt;
	GLuint txtballe;

	unsigned char *pixels, *tmppixels;

	//Load the image
	SDL_Surface *tmpballe = SDL_LoadBMP("images/ball1.bmp");
	if(tmpballe==NULL)
	{
		std::cerr << "Error while loading the ball" << std::endl;
		return false;
	}

	//creates similar RGBA surface
	SDL_Surface *balle = SDL_CreateRGBSurface(SDL_SWSURFACE, tmpballe->w, tmpballe->h, 32,
			0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
	if(balle==NULL)
	{
		std::cerr << "Error while creating surface of the ball" << std::endl;
		return false;
	}

	pixels = (unsigned char*) balle->pixels;
	tmppixels = (unsigned char*) tmpballe->pixels;

	pcnt = 0;
	cnt = 0;

	for(i=0;i<tmpballe->w;i++)
	{
		for(j=0;j<tmpballe->h;j++)
		{
			//Copy the color
			pixels[pcnt] = tmppixels[cnt];
			pixels[pcnt+1] = tmppixels[cnt+1];
			pixels[pcnt+2] = tmppixels[cnt+2];

			//magenta will be transparent
			if((tmppixels[cnt]==255)&&(tmppixels[cnt+1]==0)&&(tmppixels[cnt+2]==255))
			{
				pixels[pcnt+3] = 255;
			}
			else
			{
				pixels[pcnt+3] = 0;
			}

			cnt += 3;
			pcnt += 4;
		}
	}

	//pas the texture to OpenGL

	//generate index of the texture
	glGenTextures(1,&txtballe);

	//Choose the texture and define a simple filter for the texure
	glBindTexture(GL_TEXTURE_2D,txtballe);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

	//loading picture
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,balle->w,balle->h,0,
			GL_RGBA,GL_UNSIGNED_BYTE,balle->pixels);

	//free surfaces
	SDL_FreeSurface(balle);
	SDL_FreeSurface(tmpballe);

//	Objet::setTxtBalle(txtballe);

	return EXIT_SUCCESS;
}

int Application::Initialize(void)
{
	cout<<"Initialize..."<<endl;

	//Create context
	_Context = new Context();
	_Context->setWindow_width(800);
	_Context->setWindow_height(600);
	_Context->setFrameRate(30);

	//Create Input state
	_InputState = new InputState();

	// SDL Initialization
	if( SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		cerr << "SDL_Init failed: " << SDL_GetError() << endl;
		return (EXIT_FAILURE);
	}

	// Exit method
	atexit( SDL_Quit );

	// Set double buffer mode
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	// Set display settings
	SDL_Surface* vScreen = SDL_SetVideoMode(_Context->getWindow_width(), _Context->getWindow_height(), 32, SDL_OPENGL/*| SDL_FULLSCREEN*/);

	// Check created screen
	if( !vScreen )
	{
		cerr << "Error while creating the window: " << SDL_GetError() <<
		endl;
		return (EXIT_FAILURE);
	}

	// Set double buffer mode
	int vValue;
	if( SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &vValue) < 0)
	{
		cerr << "setting up SDL_GL_DOUBLEBUFFER has failed : " << SDL_GetError()
		<< endl;
		return (EXIT_FAILURE);
	}
	// Check double buffer mode
	if(vValue != 1)
	{
		cerr << "Erreur : SDL_GL_DOUBLEBUFFER is not active" << endl;
		return (EXIT_FAILURE);
	}

	cout<<"end of Initialize"<<endl;

	return EXIT_SUCCESS;
}
